<template>
	<view class="login-page">
		<view class="page-logo">
			<view class="welcome">
				欢迎登录 员工端
			</view>
			
			<!-- <view class="gray" @click="$to('/pages/login/register')">还没有账号？<text class="color">立即注册</text></view> -->
		</view>
		<view class="page-input">
			<view class="">
				<view class="label-tit">账号</view>
				<view class="flex align-c input-box">
					<text class="diqv">+86</text>
					<input type="number" v-model="account" maxlength="11" class="f1 input_item" placeholder="请输入手机号">
				</view>
			</view>
			<view class="">
				<view class="label-tit">密码</view>
				<view class="input-box">
					<input type="password" v-model="password" @confirm="loginBtn" class="f1 input_item ml-0" placeholder="请输入密码">
				</view>
			</view>
		</view>
		<view class="page-btn">
			<button class="btn" @click="loginBtn">登录</button>
			<!-- <view class="gray">忘记密码？</view> -->
		</view>
		<label class="page-agreement flex align-c" @click="changeAgress">
			<checkbox :checked="isAgree" color="#FF7D1B" style="transform: scale(0.6);" />我已阅读并同意<text class="color" @click.stop="getAgreement">《员工须知》</text>
		</label>
		<u-popup :show="show" mode="center"  @close="close" @open="open">
			<scroll-view scroll-y="true" class="agreement">
				<rich-text :nodes="nodes"></rich-text>
			</scroll-view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
				isAgree: false,
				nodes: '',
				show: false,
			}
		},
		onShow() {
			this.account = '';
			this.password = '';
			let token = uni.getStorageSync('token');
			let shopId = uni.getStorageSync('shopId');
			if(token) {
				if(shopId) {
					uni.switchTab({
						url: '/pages/tab/home'
					})
				}else {
					uni.redirectTo({
						url: '/pages/home/store'
					})
				}
			}
		},
		methods: {
			open() {
				this.show = true
			},
			close() {
				this.show = false
			},
			async loginBtn() {
				if(!this.account) {
					this.toast('请先输入手机号')
					return false;
				}
				if(!this.password) {
					this.toast('请输入登录密码')
					return false;
				}
				if(!this.isAgree) {
					this.toast('请先勾选协议')
					return false;
				}
				let res = await this.$api.post('staff/login', {
					mobile: this.account,
					password: this.password,
					role: 2,
				})
				if(res.code == 200) {
					uni.setStorageSync('token',res.access_token);
					// console.log(res);
					if(res.role != 2) {
						uni.redirectTo({
							url: '/pages/mine/myWallet'
						})
					}else {
						let shopId = uni.getStorageSync('shopId');
						if(shopId) {
							uni.switchTab({
								url: '/pages/tab/home'
							})
						}else {
							// #ifdef APP-PLUS
							uni.redirectTo({
								url: '/pages/home/store'
							})
							// #endif
							// #ifdef H5
							uni.navigateTo({
								url: '/pages/home/store'
							})
							// #endif
						}
					}
					
					this.toast(res.msg);
				}
			},
			changeAgress() {
				this.isAgree = !this.isAgree;
			},
			async getAgreement() {
				let res = await this.$api.get('user/agree', {
					type: 3
				});
				if(res.code == 200) {
					this.nodes = res.data.detail;
					this.open();
				}
			},
		}
	}
</script>

<style>
	page {
		background-color: #FFF;
	}
</style>

<style lang="scss" scoped>
	.agreement {
		width: 600rpx;
		padding: 25rpx;
		height: 60vh;
	}
	.color {
		color: #FF7D1B;
	}
	.gray {
		color: #B3B3B3;
	}
	.login-page {
		padding: 0 59rpx;
		background-image: linear-gradient(to bottom,#FFEFE3,#FFEFE3,#FFF,#FFF);
		.page-logo {
			padding-top: 204rpx;
			padding-bottom: 112rpx;
			font-size: 24rpx;
			.welcome {
				font-size: 40rpx;
				font-weight: bold;
			}
		}
		.page-input {
			.label-tit {
				font-size: 29rpx;
				font-weight: bold;
			}
			.diqv {
				font-size: 27rpx;
			}
			.input-box {
				margin: 30rpx 0;
				margin-top: 16rpx;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #F0F0F0;
				.input_item {
					margin-left: 30rpx;
					font-size: 27rpx;
				}
				.ml-0 {
					margin-left: 0;
				}
			}
		}
		.page-btn {
			font-size: 27rpx;
			text-align: center;
			margin: 54rpx 0;
			.btn {
				background-color: #FF7D1B;
				color: #FFF;
				margin: 0;
				padding: 0;
				height: 87rpx;
				line-height: 87rpx;
				margin-bottom: 30rpx;
			}
		}
		.page-agreement {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 95rpx;
			font-size: 27rpx;
			text-align: center;
			margin: 0 auto;
			justify-content: center;
		}
	}
</style>